package zmap

import (
	"math/rand"
	"venus/util/ztype"
)

// Keys Keys切片
func Keys[T1 comparable, T2 any](dict map[T1]T2) []T1 {
	list := make([]T1, 0)
	for k, _ := range dict {
		list = append(list, k)
	}
	return list
}

// Values Values切片
func Values[T1 comparable, T2 any](dict map[T1]T2) []T2 {
	list := make([]T2, 0)
	for _, v := range dict {
		list = append(list, v)
	}
	return list
}

// ContainValue 值等于
func ContainValue[T1 comparable, T2 comparable](dict map[T1]T2, value T2) bool {
	for _, v := range dict {
		if v == value {
			return true
		}
	}
	return false
}

// ContainValueEquals 值等于
func ContainValueEquals[T1 comparable, T2 ztype.EqualAble](dict map[T1]T2, value T2) bool {
	for _, v := range dict {
		if v.Equals(value) {
			return true
		}
	}
	return false
}

// ContainValueBy 值符合
func ContainValueBy[T1 comparable, T2 any](dict map[T1]T2, value T2, equals func(v1 T2, v2 T2) bool) bool {
	for _, v := range dict {
		if equals(v, value) {
			return true
		}
	}
	return false
}

// RandomOne 随机一个值
func RandomOne[T1 comparable, T2 any](dict map[T1]T2) T2 {
	if len(dict) <= 0 {
		return *new(T2)
	}
	list := make([]T1, 0)
	for i, _ := range dict {
		list = append(list, i)
	}
	return dict[list[rand.Intn(len(list))]]
}
